<?php

namespace App\Jobs;

use App\GroupBuyItem;
use App\Order;
use App\Services\MessageService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class GroupBuyOverJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $group_master_id;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($group_master_id)
    {
        $this->group_master_id=$group_master_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $group_items=GroupBuyItem::with('user')->where('group_master_id',$this->group_master_id)->get();
        $count=$group_items->count();
        $result="成功";
        $users=$group_items->pluck("u_nickname")->implode(",");
        $message=new MessageService();
        foreach ($group_items as $key=>$value){
            Order::where('group_item_id',$value['id'])->update(['group_status'=>1]);
            $message->send("N9Sak2TWK6lo0MT6iaMVDYNGWhu1popLaZtxA4gKdrE","pages/groupon/my",$value['user']['openid'],[
                "thing1"=>"拼团完成通知",
                "number2"=>$count,
                "phrase7"=>$result,
                "name9"=>$users
            ]);

        }

    }
}
